package ua.com.infinite.mobile.slidingmenu.fragments;

import java.util.ArrayList;

import ua.com.infinite.mobile.R;
import ua.com.infinite.mobile.adapters.PageAdapter;
import ua.com.infinite.mobile.pageindicator.TitlePageIndicator;
import ua.com.infinite.mobile.pageindicator.TitlePageIndicator.IndicatorStyle;
import ua.com.infinite.mobile.slidingmenu.lib.SlidingMenu;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.actionbarsherlock.app.SherlockFragment;

public class FragmentParentalActions extends SherlockFragment{
	
	public final static String PAGER_POSITION = "pager_position";
	public final static String PAGER_TAGS = "pager_news_tags";
	public final static String NAME_OF_FRAGMENT = "name_of_fragment";
	
	private final static String frName = "ActionsParent";
	
	protected ViewPager pager;
	protected TitlePageIndicator pageIndicator;
	protected PageAdapter adapter;
	public SlidingMenu slidingMenu;
	private int pagerPosition = 0;
	private ArrayList<String> fragmentTags;
	
	public static FragmentParentalActions newInstance(SlidingMenu slidingMenu){
		return new FragmentParentalActions(slidingMenu);
	}

	public FragmentParentalActions(SlidingMenu slidingMenu) {
		this.slidingMenu = slidingMenu;
		setRetainInstance(true);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.paging_layout, container, false);
		pager = (ViewPager)view.findViewById(R.id.pager);
		pageIndicator = (TitlePageIndicator)view.findViewById(R.id.indicator);
		
		return view;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		adapter = new PageAdapter(getActivity(), getChildFragmentManager(), pager);
		
		if (savedInstanceState != null && savedInstanceState.getString(NAME_OF_FRAGMENT).equals(frName)) {
			pagerPosition = savedInstanceState.getInt(PAGER_POSITION);
			fragmentTags = savedInstanceState.getStringArrayList(PAGER_TAGS);
			/*if (fragmentTags != null) {
				for (String tag : fragmentTags) {
					addFragmentToAdapterByTag(tag);
				}
			}*/
		} else {
			fragmentTags = new ArrayList<String>();
		}
		
		/*adapter.addTab(FragmentActions.newInstance());
		adapter.addTab(FragmentUpdates.newInstance());
		adapter.addTab(FragmentResponses.newInstance());*/
		addFragmentToAdapter(FragmentTransaktions.newInstance(TagsConst.TAG_TRANSAKTIONS), TagsConst.TAG_TRANSAKTIONS);
		
		pager.setAdapter(adapter);
		
		pageIndicator.setOnPageChangeListener(pageChangeListener);
		final float density = getResources().getDisplayMetrics().density;

		pageIndicator.setBackgroundColor(getResources().getColor(R.color.disabled));
		pageIndicator.setFooterColor(0xFFAA2222);
		pageIndicator.setFooterLineHeight(0); // 1dp
		pageIndicator.setFooterIndicatorHeight(3 * density); // 3dp
		pageIndicator.setFooterIndicatorStyle(IndicatorStyle.None);
		pageIndicator.setTextColor(0xAA000000);
		pageIndicator.setSelectedColor(0xFF000330);
		pageIndicator.setSelectedBold(true);

		pageIndicator.setViewPager(pager);
		if(adapter.getCount()<1){
			pageIndicator.setVisibility(TitlePageIndicator.GONE);
		}else{
			pageIndicator.setVisibility(TitlePageIndicator.VISIBLE);
		}
		
		slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt(PAGER_POSITION, pagerPosition);
		outState.putString(NAME_OF_FRAGMENT, frName);
		outState.putStringArrayList(PAGER_TAGS, fragmentTags);
	}
	
	private void addFragmentToAdapter(Fragment content, String tag) {
		adapter.addTab(content);
		fragmentTags.add(tag);
		//getActivity().getSupportFragmentManager().beginTransaction().add(content, tag).commit();
		adapter.notifyDataSetChanged();
	}
	
	protected OnPageChangeListener pageChangeListener = new OnPageChangeListener() {

		@Override
		public void onPageSelected(int position) {
			if (position == 0) {
				slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
			} else {
				slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
			}
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {

		}

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}
	};
	
	
}
